using DG.Tweening;
using Spine;
using Spine.Unity;
using UnityEngine;

public class PlayerZedController : MonoBehaviour
{
	private readonly string[] AnimationName = new string[12]
	{
		"idle",
		"hit",
		"walk",
		"attack1",
		"attack2",
		"attack3",
		"attack4",
		"skill1",
		"skill2",
		"attack5",
		"die",
		"slide"
	};

	private SkeletonAnimation skeletonAnimationHead;

	private float distance;

	private float distance1;

	private GameObject _target;

	private GameObject _target1;

	private bool attack;

	private bool skill1;

	private bool skill2;

	private int go = -1;

	private int a;

	private bool isComplete = true;

	public GameObject Effatk5Zed;

	public GameObject Effskill1Zed;

	public Transform AttackZed;

	public bool isright;

	private bool chuyen;

	public int damagePlayer;

	private void Start()
	{
		skeletonAnimationHead = GetComponent<SkeletonAnimation>();
		skeletonAnimationHead.state.Complete += AnimationComplete;
		skeletonAnimationHead.state.Event += ActiveSkill;
		if (!isright)
		{
			base.transform.localScale = new Vector3(-1f, 1f, 1f);
		}
		else
		{
			base.transform.localScale = new Vector3(1f, 1f, 1f);
		}
	}

	private void Update()
	{
		if (_target == null)
		{
			FindEnemy();
		}
		else
		{
			distance = Vector3.Distance(base.transform.position, _target.transform.position);
		}
		if (_target1 == null)
		{
			FindBoom();
		}
		else
		{
			distance1 = Vector3.Distance(base.transform.position, _target1.transform.position);
		}
		if (go == 1 && !skill1 && !skill2)
		{
			base.transform.localScale = new Vector3(1f, 1f, 1f);
			if (!attack)
			{
				isright = true;
				SetAnim(AnimationName[0], loop: true);
			}
		}
		if (go == 0)
		{
			if (!skill1 && !skill2)
			{
				base.transform.localScale = new Vector3(-1f, 1f, 1f);
				if (!attack)
				{
					isright = false;
					SetAnim(AnimationName[0], loop: true);
				}
			}
		}
		else if (go == -1 && !attack && !skill1 && !skill2 && isComplete)
		{
			SetAnim(AnimationName[0], loop: true);
			chuyen = false;
		}
		if (attack && !skill1 && !skill2)
		{
			if (a == 0 && !chuyen)
			{
				SetAnim(AnimationName[3], loop: false);
				isComplete = true;
			}
			else if (a == 1 && chuyen && !isComplete)
			{
				SetAnim(AnimationName[4], loop: false);
				isComplete = true;
			}
			else if (a == 2 && chuyen && !isComplete)
			{
				SetAnim(AnimationName[5], loop: false);
				isComplete = true;
			}
			else if (a == 3 && chuyen && !isComplete)
			{
				SetAnim(AnimationName[6], loop: false);
				isComplete = true;
			}
			else if (a == 4 && chuyen && !isComplete)
			{
				SetAnim(AnimationName[9], loop: false);
				isComplete = true;
			}
			else if (!isComplete)
			{
				SetAnim(AnimationName[3], loop: false);
			}
		}
		if (skill1 && !skill2 && isComplete)
		{
			SetAnim(AnimationName[7], loop: false);
			isComplete = false;
		}
		if (skill2 && !skill1 && isComplete)
		{
			SetAnim(AnimationName[8], loop: false);
			isComplete = false;
		}
	}

	private void SetAnim(string nameAnim, bool loop)
	{
		if (skeletonAnimationHead.AnimationName != nameAnim)
		{
			skeletonAnimationHead.state.SetAnimation(0, nameAnim, loop);
		}
	}

	public void OnclickSkill1()
	{
		skill1 = true;
	}

	public void OnclickSkill2()
	{
		skill2 = true;
	}

	public void OnclickAttack()
	{
		attack = true;
		isComplete = false;
	}

	public void Playergoleft()
	{
		go = 0;
		isright = false;
	}

	public void Playergoright()
	{
		go = 1;
		isright = true;
	}

	public void PlayerIdle()
	{
		go = -1;
	}

	private void AnimationComplete(TrackEntry entry)
	{
		attack = false;
		skill1 = false;
		skill2 = false;
		isComplete = true;
		a = 0;
	}

	private void ActiveSkill(TrackEntry entry, Spine.Event e)
	{
		if (e.Data.name == "attack")
		{
			if (entry.animation.name == "attack1" || entry.animation.name == "attack2" || entry.animation.name == "attack4")
			{
				attack123none();
			}
			else if (entry.animation.name == "attack3")
			{
				attack3Zed();
			}
			else if (entry.animation.name == "attack5")
			{
				attack5Zed();
			}
			else if (entry.animation.name == "skill1")
			{
				skill1Zed();
			}
		}
		else if (e.ToString() == "chuyen")
		{
			a++;
			chuyen = true;
		}
	}

	private void FindEnemy()
	{
		for (int i = 0; i < GameManager.AllEnemy.Count; i++)
		{
			if ((bool)GameManager.AllEnemy[i])
			{
				distance = Vector3.Distance(base.transform.position, GameManager.AllEnemy[i].transform.position);
				if (distance < 10f)
				{
					_target = GameManager.AllEnemy[i];
				}
			}
		}
	}

	private void FindBoom()
	{
		for (int i = 0; i < GameManager.AllBoom.Count; i++)
		{
			if ((bool)GameManager.AllBoom[i])
			{
				distance1 = Vector3.Distance(base.transform.position, GameManager.AllBoom[i].transform.position);
				if (distance1 < 2f)
				{
					_target1 = GameManager.AllBoom[i];
				}
			}
		}
	}

	private void attack123none()
	{
		for (int i = 0; i < GameManager.AllEnemy.Count; i++)
		{
			if (!GameManager.AllEnemy[i])
			{
				continue;
			}
			distance = Vector3.Distance(base.transform.position, GameManager.AllEnemy[i].transform.position);
			if (distance < 2.2f)
			{
				Vector3 position = GameManager.AllEnemy[i].transform.position;
				float x = position.x;
				Vector3 position2 = base.transform.position;
				if (x < position2.x && !isright)
				{
					GameManager.AllEnemy[i].gameObject.GetComponent<EnemyController>().Hit_Player(damagePlayer, a: false, 1);
					continue;
				}
			}
			if (distance < 2.2f)
			{
				Vector3 position3 = GameManager.AllEnemy[i].transform.position;
				float x2 = position3.x;
				Vector3 position4 = base.transform.position;
				if (x2 >= position4.x && isright)
				{
					GameManager.AllEnemy[i].gameObject.GetComponent<EnemyController>().Hit_Player(damagePlayer, a: false, 1);
				}
			}
		}
		for (int j = 0; j < GameManager.AllBoom.Count; j++)
		{
			if (!GameManager.AllBoom[j])
			{
				continue;
			}
			distance1 = Vector3.Distance(base.transform.position, GameManager.AllBoom[j].transform.position);
			if (_target1 != null && distance1 < 2.2f)
			{
				Vector3 position5 = GameManager.AllBoom[j].transform.position;
				float x3 = position5.x;
				Vector3 position6 = base.transform.position;
				if (x3 >= position6.x && isright)
				{
					_target1.transform.DOMove(new Vector3(18.8f, 0.8f, 0f), 1f);
				}
			}
		}
	}

	private void attack3Zed()
	{
		for (int i = 0; i < GameManager.AllEnemy.Count; i++)
		{
			if ((bool)GameManager.AllEnemy[i])
			{
				distance = Vector3.Distance(base.transform.position, GameManager.AllEnemy[i].transform.position);
				if (distance < 2.4f)
				{
					GameManager.AllEnemy[i].gameObject.GetComponent<EnemyController>().Hit_Player(damagePlayer, a: true, 0);
				}
			}
		}
	}

	private void attack5Zed()
	{
		if (isright)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(Effatk5Zed, AttackZed.position, Quaternion.identity);
			gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500f);
			gameObject.GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
			UnityEngine.Object.Destroy(gameObject, 0.35f);
		}
		else
		{
			GameObject gameObject2 = UnityEngine.Object.Instantiate(Effatk5Zed, AttackZed.position, Quaternion.identity);
			gameObject2.GetComponent<Rigidbody2D>().AddForce(-Vector2.right * 500f);
			gameObject2.GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
			UnityEngine.Object.Destroy(gameObject2, 0.35f);
		}
	}

	private void skill1Zed()
	{
		if (isright)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(Effskill1Zed, AttackZed.position, Quaternion.identity);
			gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 500f);
			gameObject.GetComponent<SkeletonAnimation>().skeleton.FlipX = false;
			UnityEngine.Object.Destroy(gameObject, 0.5f);
		}
		else
		{
			GameObject gameObject2 = UnityEngine.Object.Instantiate(Effskill1Zed, AttackZed.position, Quaternion.identity);
			gameObject2.GetComponent<Rigidbody2D>().AddForce(-Vector2.right * 500f);
			gameObject2.GetComponent<SkeletonAnimation>().skeleton.FlipX = true;
			UnityEngine.Object.Destroy(gameObject2, 0.5f);
		}
	}
}
